//
//  OO_ToBePaidViewController.m
//  ShopProject
//
//  Created by macbookpro on 2018/10/2.
//  Copyright © 2018年 ShopProject. All rights reserved.
//

#import "OO_ToBePaidViewController.h"
#import "OO_ToBeCell.h"
#import "OO_PayViewController.h"
@interface OO_ToBePaidViewController ()<UITableViewDelegate,UITableViewDataSource,OO_ToBeCellDelgate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataSource;
@end

@implementation OO_ToBePaidViewController
-(NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    
    return _dataSource;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"OO_ToBeCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"OO_ToBeCell"];
    WeakSelf
    /*只有一组数据，不要上拉加载*/
    [self.tableView setRefreshWithHeaderBlock:^{
        [weakSelf loadData ];
    } footerBlock:nil];
    [self.tableView.mj_header beginRefreshing];
    [self.tableView setupEmptyDataText:@"暂无订单,点击重试" verticalOffset:0 emptyImage:[UIImage imageNamed:@"nodata"] tapBlock:^{
        [weakSelf.tableView.mj_header beginRefreshing ];
    }];
}


-(void)loadData{
    
    WeakSelf
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self.dataSource removeAllObjects];
        
        JQFMDB *db = [JQFMDB shareDatabase];
        NSMutableArray *dataArr = [NSMutableArray new];
         [dataArr addObjectsFromArray: [db jq_lookupTable:TOBEPAY dicOrModel:[OO_OrderModel class] whereFormat:@"where logionUser = %@",[UserDefaults readFromPlist:LOGIN_STATUS_TEL]]];
          dataArr =(NSMutableArray *)[[dataArr reverseObjectEnumerator] allObjects];
        [self.dataSource addObjectsFromArray:dataArr];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [weakSelf.tableView reloadData];
            [weakSelf.tableView.mj_header endRefreshing];
        });
        
        
    });
    
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    OO_ToBeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"OO_ToBeCell"];
    cell.model = self.dataSource[indexPath.row];
    cell.delegate = self;
    cell.payWayLable.hidden = YES;
    return cell;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140.0f;
    
}
-(void)operationButtonClickedAtCell:(UITableViewCell *)cell{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    if (indexPath) {
        
        WeakSelf
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确认删除该订单吗？" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
            
            [weakSelf deleteOrder:weakSelf.dataSource[indexPath.row]];
            
            
            
        }];
        UIAlertAction * cancelAction =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        [alert addAction:okAction];
        [alert addAction:cancelAction];
        [self presentViewController:alert animated:true completion:nil];
        

        
    }
    
    
}
-(void)deleteOrder:(OO_OrderModel *)model{
    
    WeakSelf
    [MBProgressHUD showActivityMessageInView:@"请稍等..."];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        
        JQFMDB *db = [JQFMDB shareDatabase];
        [db jq_deleteTable:TOBEPAY whereFormat:@"where ID = '%@'",model.ID];
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [MBProgressHUD hideHUD];
            [MBProgressHUD showTipMessageInWindow:@"订单删除成功"];
            [weakSelf loadData];
            
        });
        
        
    });
    
    
}
-(void)payButtonClickedAtCell:(UITableViewCell *)cell{
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    if (indexPath) {
        OO_PayViewController *vc = [OO_PayViewController new];
//        vc.allMoney = weakSelf.allMone
        OO_OrderModel *model = self.dataSource[indexPath.row];
        NSMutableArray *orderArr = [NSMutableArray new];
        [orderArr addObject:model];
        vc.allMoney = [model.price floatValue] * model.goodsNumber;
        vc.orderArr = orderArr;
        LEINavigationController *nav = [[LEINavigationController alloc] initWithRootViewController:vc];
        
        [self.navigationController presentViewController:nav animated:1 completion:^{
            [self.navigationController popToRootViewControllerAnimated:YES];
        }];
    }
    
    
}
@end
